package com.itheima.edu.info.manager.dao.impl;

import com.itheima.edu.info.manager.dao.BaseTeacherDao;
import com.itheima.edu.info.manager.domain.Teacher;

public class TeacherDao implements BaseTeacherDao {
    static final int LIST_LENGTH = 5;
    private static Teacher[] teachers = new Teacher[LIST_LENGTH];
    static {
        Teacher teacher = new Teacher("heima001","张三","30岁","1980-11-11");
        Teacher teacher1 = new Teacher("heima002","李斯","29岁","1981-11-11");
        teachers[0] = teacher;
        teachers[1] = teacher1;
        System.out.println("----------teacher数组初始化完成----------");
    }
    public boolean addTeacher(Teacher teacher) {
        int index = -1;
        for (int i = 0; i < teachers.length; i++) {
            if (teachers[i] == null){
                teachers[i] = teacher;
                return true;
            }
        }
        return false;
    }

    public Teacher[] findAllTeacher() {
        return teachers;
    }

    public Teacher selectTeacher(String id) {
        for (int i = 0; i < teachers.length; i++) {
            if (teachers[i] != null && teachers[i].getId().equals(id))
                return teachers[i];
        }
        return null;
    }

    public Teacher updataTeacher(Teacher teacher) {
        for (int i = 0; i < teachers.length; i++) {
            if (teachers[i] != null && teachers[i].getId().equals(teacher.getId())){
                teachers[i].setName(teacher.getName());
                teachers[i].setAge(teacher.getAge());
                teachers[i].setBirthday(teacher.getBirthday());
                return teachers[i];
            }
        }
        return null;
    }

    public boolean delectTeacher(String id) {
        //遍历老师数组
        for (int i = 0; i < teachers.length; i++) {
            if (teachers[i] != null && teachers[i].getId().equals(id)) {
                teachers[i] = null;
                return true;
            }
        }
        //不存在返回false
        return false;
    }
}
